package com.itheima.rabbitmqproducerspringboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //交换机名称
    public static final String EXCHANGE_NAME = "test_exchange_dlx";
    //队列名称
    public static final String QUEUE_NAME = "test_queue_dlx";

    //声明普通交换机
    @Bean("test_exchange_dlx")
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //声明普通队列
    @Bean("test_queue_dlx")
    public Queue itemQueue() {
        return QueueBuilder
                .durable(QUEUE_NAME)
                .withArgument("x-message-ttl",5000)
                .withArgument("x-max-length",10)
                .withArgument("x-dead-letter-exchange","exchange_dlx")
                .withArgument("x-dead-letter-routing-key","dlx.abc")
                .build();
    }

    //绑定普通交换机和队列
    @Bean
    public Binding itemQueueExchange(@Qualifier("test_queue_dlx") Queue queue, @Qualifier("test_exchange_dlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("test.dlx.#").noargs();
    }

    //声明死信交换机
    @Bean("exchange_dlx")
    public Exchange exchange_dlx() {
        return ExchangeBuilder.topicExchange("exchange_dlx").durable(true).build();
    }

    //声明死信队列
    @Bean("queue_dlx")
    public Queue queue_dlx() {
        return QueueBuilder
                .durable("queue_dlx")
                .build();
    }

    //绑定死信交换机和队列
    @Bean
    public Binding DLXBindQueueExchange(@Qualifier("queue_dlx") Queue queue, @Qualifier("exchange_dlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }


}